import yaml

yaml.warnings({'YAMLLoadWarning': False})
from dataconfig.file_config import FilePath
from logs.user_log import UserLog
# from ruamel import yaml


class OperationYaml(object):
    def __init__(self, file_path=None):
        if file_path == None:
            self.file_path = FilePath().config_json()
        else:
            self.file_path = file_path
        self.log = UserLog().get_log()

    # 读取文件
    def read_data(self):
        file_path = self.file_path
        with open(file_path, encoding="utf-8") as file_01:
            # result = yaml.load(file_01, Loader=yaml.Loader)
            result = yaml.load(file_01)
        return result


    # 写json,str格式
    def write_data(self, data, file_path=None):
        self.log.debug('准备写入更新后数据')
        if file_path == None:
            file_path = self.file_path
        else:
            file_path = file_path
        with open(file_path, 'w', encoding='utf-8') as fi:
            # yaml.dump(data, fi, Dumper=yaml.RoundTripDumper)
            yaml.dump(data, fi, allow_unicode=True)
            self.log.debug('写入成功')


if __name__ == '__main__':
    pass
    read_init = OperationYaml()
    a02 = read_init.read_data()
    a02['cancel_order'] = '哈哈哈'
    read_init.write_data(a02)
    # print(a02)
    # print(type(a02))

